home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ptv1n4.arc / TSRMEM2.C < prev    next >
Text File  |  1990-09-13  |  2KB  |  103 lines

  1. /*
  2. These variables define the stack segment and offset
  3. to be used by the TSR.
  4. */
  5. unsigned my_ss, my_sp;
  6.  
  7. /*
  8. Minimum number of paragraphs required for TSR's
  9. stack.
  10. */
  11. #define MIN_STACK  0x40
  12.  
  13. /*
  14. Install a TSR.
  15. Keep enough memory for the dynamic heap and stack.
  16. The 'heap' and 'stack' parameters are the number of
  17. paragraphs required by the heap and stack
  18. respectively.
  19. */
  20. void install_tsr(unsigned heap, unsigned stack)
  21. {
  22. unsigned PSP, EODATA, keeplen;
  23.  
  24. /*
  25. Get beginning and end of program memory.
  26. */
  27. PSP = GetPSP();
  28. EODATA = EndPrg();
  29.  
  30. /*
  31. Make sure we have enough stack space.
  32. */
  33. stack = max(MIN_STACK, stack);
  34.  
  35. /*
  36. Amount of memory required is (program size + heap +
  37. stack).
  38. */
  39. keeplen = EODATA - PSP + heap + stack;
  40.  
  41. /*
  42. Stack comes after data segment and heap and grows
  43. down in memory.
  44. */
  45. my_ss = EODATA;
  46. my_sp = (heap + stack) * 16;
  47.  
  48. /*
  49. keep(status, size) is a Turbo C function that keeps
  50. 'size' paragraphs in memory and exits to DOS with
  51. status 'status'.  It uses DOS function 0x31.
  52. */
  53. keep(0, keeplen);
  54. }
  55.  
  56. /*
  57. Initialize TSR stack, run the TSR, restore
  58. interrupted program's stack, and return control to
  59. interrupted program.
  60. */
  61. void init_tsr(void)
  62. {
  63. /*
  64. These must be static variables because local
  65. variables are addressed from the stack, which we
  66. will be changing.
  67. */
  68. static unsigned old_ss, old_sp;
  69.  
  70. /*
  71. Save old stack frame.  _SS and _SP are Turbo C
  72. pseudo-variables that directly address the SS and SP
  73. registers.
  74. */
  75. old_ss = _SS;
  76. old_sp = _SP;
  77.  
  78. /*
  79. Set up the TSR stack.  disable() and enable() are
  80. Turbo C pseudo-functions that disable and enable
  81. interrupts.  The last thing we want is for an
  82. interrupt to occur while we're changing the stack!
  83. */
  84. disable();
  85. _SS = my_ss;
  86. _SP = my_sp;
  87. enable();
  88.  
  89. /*
  90. run_tsr() is user-defined, i.e. implemented by the
  91. programmer.
  92. */
  93. run_tsr();
  94.  
  95. /*
  96. Restore original program's stack.
  97. */
  98. disable();
  99. _SS = old_ss;
  100. _SP = old_sp;
  101. enable();
  102. }
  103.